#网络编程

#服务端代码
# import socket
# # 1.监听本机的IP和端口
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# sock.bind(('127.0.0.1', 8001)) # IP,端口
# sock.listen(5) # 支持排队等待5人
# while True:
#     # 2.等待，有人来连接（阻塞）
#     conn, addr = sock.accept() # 等待客户端来连接（阻塞）
#     # 3.等待，连接者发送消息（阻塞）
#     client_data = conn.recv(1024) # 等待接收客户端发来数据
#     print(client_data.decode('utf-8')) # 字节
#     # 4.给连接者回复消息
#     conn.sendall("hello world".encode('utf-8'))
#     # 5.关闭连接
#     conn.close()
# # 6.停止服务端程序
# sock.close()


#客户端
# import socket
# # 1. 向指定IP发送连接请求
# client = socket.socket()
# client.connect(('127.0.0.1', 8001)) # 向服务端发起连接（阻塞）10s
# # 2. 连接成功之后，发送消息
# client.sendall("hello xiaofeng".encode('utf-8'))
# # 3. 等待，消息的回复（阻塞）
# reply = client.recv(1024)
# print(reply)
# # 4. 关闭连接
# client.close()

#案例：智障客服
# import socket
# # 1.监听本机的IP和端口
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# sock.bind(('127.0.0.1', 8001))  # 127.0.0.1 或 查看自己局域网本地IP地址
# sock.listen(5)
# while True:
#     # 2.等待，有人来连接（阻塞）
#     conn, addr = sock.accept()
#     print("有人来连接了...")
#     # 3.连接成功后立即发送
#     conn.sendall("欢迎使用xx系统，请输入您想要办理的业务！".encode("utf-8"))
#     while True:
#         # 3.等待接受信息
#         data = conn.recv(1024)
#         if not data:
#             break
#         data_string = data.decode("utf-8")
#         print("客户端提问：", data_string)
#         # 4.回复消息
#         conn.sendall("你说啥？".encode("utf-8"))
#     print("断开连接了")
#     # 5.关闭与此人的连接
#     conn.close()
# # 6.停止服务端程序
# sock.close()

#客户端
# import socket
# # 1. 向指定IP发送连接请求
# client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# client.connect(('127.0.0.1', 8001))
# # 2.连接成功后，获取系统登录信息
# message = client.recv(1024)
# print(message.decode("utf-8"))
# while True:
#     content = input("请输入(q/Q退出)：")
#     if content.upper() == 'Q':
#         break
#     client.sendall(content.encode("utf-8"))
#     # 3. 等待，消息的回复
#     reply = client.recv(1024)
#     print(reply.decode("utf-8"))
# # 关闭连接，关闭连接时会向服务端发送空数据。
# client.close()


#案例：文件上传
#服务端
# import socket
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# sock.bind(('127.0.0.1', 8001))  # 127.0.0.1 或 查看自己局域网本地IP地址
# sock.listen(5)
# conn, addr = sock.accept()
# # 接收文件大小
# data = conn.recv(1024)
# total_file_size = int(data.decode('utf-8'))
# # 接收文件内容
# file_object = open('xxx.png', mode='wb')
# recv_size = 0
# while True:
#     # 每次最多接收1024字节
#     data = conn.recv(1024)
#     file_object.write(data)
#     file_object.flush()
#     recv_size += len(data)
#     # 上传完成
#     if recv_size == total_file_size:
#         break
# # 接收完毕，关闭连接
# conn.close()
# sock.close()
#客户端
# import time
# import os
# import socket
# client = socket.socket()
# client.connect(('127.0.0.1', 8001))
# file_path = input("请输入要上传的文件：")
# # 先发送文件大小
# file_size = os.stat(file_path).st_size
# client.sendall(str(file_size).encode('utf-8'))
# print("准备...")
# time.sleep(2)
# print("开始上传..")
# file_object = open(file_path, mode='rb')
# read_size = 0
# while True:
#     chunk = file_object.read(1024) # 每次读取1024字节
#     client.sendall(chunk)
#     read_size += len(chunk)
#     if read_size == file_size:
#         break
# client.close()

